import { toast,dateFormat} from '../../../utils/util'
import {queryUserInfo} from '../../api/insurance'
import dayjs from 'dayjs'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        date:'',
        datec:'',
        datec2:'',
        id:'',
        userId: '',
        phone:'',
        userName:'',
        comboName:'',
        outDangerDate:'',
        isTips:true,
        idCardNo:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options)
        const date = dayjs(options.createtime);
        const previousDate = date.add(1, 'day').format('YYYY-MM-DD');
        if (options.name){
            this.setData({
                id:options.id,
                userId: options.user,
                phone:options.phone,
                userName:options.name,
                comboName:options.comboname,
                datec:previousDate,
                datec2:dayjs(options.createtime).format('YYYY-MM-DD'),
                date: dayjs(options.createtime).format('YYYY-MM-DD'),
                outDangerDate:options.createtime,
                idCardNo:options.idCardNo
            })
        }
        // const time =  dateFormat('YYYY-mm-dd',new Date())
        // this.setData({
        //     date:time
        // })
        // this.getQueryUserInfo()
    },
    bindDateChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            date: e.detail.value
        })
        // this.getQueryUserInfo()
    },
    getQueryUserInfo(){
        queryUserInfo({
            insureDay:this.data.date,
            outDangerUserName:'',
            insureCompanyType:'insure_type_tyrb'
        }).then(res=>{
            if(res.length === 0){
                this.setData({
                    isTips:true
                })
            }else{
                this.setData({
                    isTips:false
                })
            }
        })
    },
    onSearch(){
        if(this.data.date){
            wx.navigateTo({
                url: '/packageMicroInsurance/pages/insurance-search/insurance-search?outDangerDate=' + this.data.date ,
                events: {
                    // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
                    someEvent: (data)=> {
                        console.log(data,'data')
                        if (data.name){
                            this.setData({
                                id:data.id,
                                userId: data.user,
                                phone:data.phone,
                                userName:data.name,
                                comboName:data.comboname,
                                date:data.outDangerDate,
                                outDangerDate:data.outDangerDate,
                                idCardNo:data.idCardNo
                            })
                        }
                    },
                }
            })
        }else{
            toast('请先选择日期')
        }
    },
    onReportCase(){
        console.log(this.data)
        if(!this.data.userName){
            toast('请选择出险人')
            return
        }
        if(!this.data.date){
            toast('请先选择日期')
            return
        }
        // if(this.data.date !== this.data.outDangerDate){
        //     toast('出险日期必须和出险人日期一样')
        //     return
        // }
        wx.navigateTo({
            url: '/packageMicroInsurance/pages/insurance-report-detail/insurance-report-detail',
            success: (res)=>{
                res.eventChannel.emit('init', { 
                    id: this.data.id,
                    userId: this.data.userId,
                    phone: this.data.phone,
                    userName: this.data.userName,
                    comboName: this.data.comboName,
                    date: this.data.date,
                    idCardNo:this.data.idCardNo
                })
            }
        })
        
    }
})